<?php

require_once dirname(__FILE__).'/../lib/fitGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/fitGeneratorHelper.class.php';

/**
 * fit actions.
 *
 * @package    maslogger
 * @subpackage fit
 * @author     Michaël VEROUX
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fitActions extends autoFitActions
{

     public function executeAjaxSelect(sfWebRequest $request)
     {
         $this->getResponse()->setContentType('application/json');

         $string = $request->getParameter('q');
         $req = Doctrine::getTable('MlFit')->getAjaxSearch($string);

         $results = array();
         foreach ( $req as $key => $result ){
          $results[$result['MlIncidents']['MlMachines']->getNumero()] = 'Machine n°'.$result['MlIncidents']['MlMachines']->getNumero().' - socle n°'.$result['MlIncidents']['MlMachines']->getSocle();
     }
         
         if(count($results)<=0){
         	$results[0] = 'Aucun résultat !';
         }

         return $this->renderText(json_encode($results));
     }	
    public function executeDeclare(sfWebRequest $request){
      //$this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("numero"));

	
	/*********************************** POUR LA LISTE ********************************************/
	
	$filtre = array();
	$filtre = $request->getParameter('ml_fit_filters');
	$filtre['numero']['text'] = $number;

	$request->setParameter('ml_fit_filters',$filtre);
     
	$this->is_partial = true;
    $this->executeFilter($request);
    
    $myfit = Doctrine::getTable('MlFit')->fillFit();
    if(isset($myfit[$number])){
    	$this->fit = $myfit[$number];
    }else{
    	$this->fit = array();
    }
	/*************************************************************************************************/
     return $this->renderPartial('ajaxList',array('pager' => $this->pager, 'fit' => $this->fit, 'sort' => $this->sort, 'ml_fit' => $this->fit, 'configuration' => $this->configuration, 'helper' => $this->helper));
    }  
  public function executeIndex(sfWebRequest $request)
  {
  	parent::executeIndex($request);
  	return $this->executeDeclare($request);
  }       
  public function executeFilter(sfWebRequest $request)
  {
    $this->setPage(1);

    if ($request->hasParameter('_reset'))
    {
      $this->setFilters($this->configuration->getFilterDefaults());

		if(!isset($this->is_partial)){
	      $this->redirect('@ml_fit');
		}
    }

    $this->filters = $this->configuration->getFilterForm($this->getFilters());

    $this->filters->bind($request->getParameter($this->filters->getName()));
    if ($this->filters->isValid())
    {
      $this->setFilters($this->filters->getValues());

		if(!isset($this->is_partial)){
	      $this->redirect('@ml_fit');
		}
    }
    $this->pager = $this->getPager();
    $this->sort = $this->getSort();
	if(!isset($this->is_partial)){
		$this->setTemplate('index');
	}
  }
  private function makePdf($ids){
  	$myData = MlFitTable::getInstance();
  	//$myData = Doctrine_Core::getTable('MlFit')->getMyFit($request->getParameter('id'));

  	$pdf=new PDF('l');
  	foreach($ids AS $id){
	  	$pdf->setDatas($myData->getMyFit($id));
	  	$pdf->SetAutoPageBreak(true,3);
	  	$pdf->AliasNbPages();
		$pdf->AddPage();
		$pdf->myPage();
  	}
	$pdf->Output();
  }
  public function executeListImprimer(sfWebRequest $request){
  	$this->makePdf(array($request->getParameter('id')));
  }
  protected function executeBatchImprimer(sfWebRequest $request)
  {
    $ids = $request->getParameter('ids');
	$this->makePdf($ids);
  }  
}
